from django.db import models


# 设计 广告分类表

class AdvertCate(models.Model):
    name = models.CharField(max_length=20)

    class Meta:
        db_table = "advert_cate"


class Advert(models.Model):
    title = models.CharField('标题', max_length=120)
    pic = models.ImageField(upload_to="advert", null=True, blank=True)
    url = models.CharField(max_length=120)
    sorted = models.SmallIntegerField('广告的排序值')
    cate = models.ForeignKey(AdvertCate, on_delete=models.CASCADE)

    class Meta:
        db_table = "adverts"


# 设计一个分类组
class CategoryGroup(models.Model):
    name = models.CharField(max_length=20)
    sorted = models.SmallIntegerField(default=1, verbose_name="顺序")


# 设计商品分类

class Category(models.Model):
    name = models.CharField(max_length=20, verbose_name="分类名")
    parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, verbose_name="上级分类")
    pic = models.ImageField(upload_to='cates', verbose_name="分类封面图片", null=True, blank=True)
    level = models.SmallIntegerField('级别')
    group = models.ForeignKey(CategoryGroup, on_delete=models.CASCADE)

    class Meta:
        db_table = "category"

    def __str__(self):
        return self.name + str(self.level)


# 编写model模型
class Goods(models.Model):
    name = models.CharField("商品名", max_length=20)
    price = models.DecimalField("价格", max_digits=7, decimal_places=2)
    comments = models.IntegerField('评论量', default=0)
    category1 = models.ForeignKey(Category, related_name="book1", on_delete=models.SET_NULL, null=True)
    category2 = models.ForeignKey(Category, related_name="book2", on_delete=models.SET_NULL, null=True)
    pic = models.ImageField("图片", upload_to="goods")
    sale = models.IntegerField("销量", default=0)
    stock = models.IntegerField('库存')
    desc = models.CharField("简介", max_length=120)
    content = models.TextField('详情')

    class Meta:
        db_table = "goods"
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
